// 引入Sequelize模块
const Sequelize = require('sequelize');
// 引入数据库实例
const db = require('../db');
const user = require('./user');

//一级主产品
const good = db.define("good", {
    // 主键
    objectId: {
        type: Sequelize.STRING(20),
        allowNull: false,
        unique: true,
        primaryKey: true
    },

    goodsName: {
        type: Sequelize.STRING,
        allowNull: false,
        comment: "名称"
    },

    brand: {
        type: Sequelize.STRING(20),
        comment: "品牌"
    },

    packModel:{
        type: Sequelize.STRING(100),
        comment: "规格"
    },

    minPrice: {
        type: Sequelize.INTEGER,
        comment: "最低价格"
    },

    maxPrice: {
        type: Sequelize.INTEGER,
        comment: "最高价格"
    },

    sellNum:{
        type: Sequelize.INTEGER,
        comment: "销量"
    },

    priceDesc:{
        type: Sequelize.VIRTUAL,
        comment: "价格描述",
        get() {
            let minPrice = this.getDataValue('minPrice');
            let maxPrice = this.getDataValue('maxPrice');
            let returnAge
            if (maxPrice) {
                returnAge = minPrice + "~"
                if (maxPrice) returnAge += maxPrice
            } else {
                returnAge = minPrice
            }
            return returnAge
        }
    },

    goodClassId: {
        type: Sequelize.STRING,
        comment: "分类id"
    },

    goodsIcon: {
        type: Sequelize.STRING(100),
        comment: "商品主图",
        get() {
            let goodsIcon = this.getDataValue('goodsIcon');
            let subGoodIcons = this.getDataValue('subGoodIcons') || [];
            if (subGoodIcons && subGoodIcons.length > 0) {
                goodsIcon = subGoodIcons[0]
            } else {
                goodsIcon = "https://kcb-1306121076.cos.ap-shanghai.myqcloud.com/ea567360-ef6d-11eb-96f0-f5e1ee6b1ebf.png/sf"
            }
            return goodsIcon
        }
    },

    subGoodIcons: {
        type: Sequelize.JSON,
        defaultValue: [],
        comment: "商品图"
    },

    tags: {
        type: Sequelize.JSON,
        defaultValue: [],
        comment: "商品标签"
    },

    productId: {
        type: Sequelize.STRING(30),
        comment: "组件商品id"
    },

    status: {
        type: Sequelize.INTEGER,
        defaultValue: 1,
        comment: "是否已审核，1表示已上架，-1表示未上架",
    },

    isNice: {
        type: Sequelize.BOOLEAN,
        defaultValue: false,
        comment: "是否是精选内容"
    },

    statusDesc: {
        type: Sequelize.VIRTUAL,
        get() {
            const status = this.getDataValue('status');
            if (status == 1) {
                return "已审核"
            } else if (status == 0) {
                return "未审核"
            }
        }
    },

    createdAt: {
        type: Sequelize.DATE,
        get() {
            const rawValue = this.getDataValue('createdAt');
            return dateFormat(rawValue, 'yyyy-mm-dd HH:MM:ss');
        }
    },

    updatedAt: {
        type: Sequelize.DATE,
        get() {
            const rawValue = this.getDataValue('updatedAt');
            return dateFormat(rawValue, 'yyyy-mm-dd HH:MM:ss');
        }
    },
})


// 一级分类model
const goodClass = db.define('goodClass', {
    // 主键
    objectId: {
        type: Sequelize.STRING(20),
        allowNull: false,
        unique: true,
        primaryKey: true
    },

    name: {
        type: Sequelize.STRING(50),
        allowNull: false,
    },

    beizhu: {
        type: Sequelize.STRING(100),
        allowNull: true,
        defaultValue: 0,
    },

    orderNum: {
        type: Sequelize.INTEGER,
        defaultValue: 0,
        comment: "排序值"
    },

    isSee: {
        type: Sequelize.BOOLEAN,
        defaultValue: true,
        comment: "是否可见"
    },

    createdAt: {
        type: Sequelize.DATE,
        get() {
            const rawValue = this.getDataValue('createdAt');
            return dateFormat(rawValue, 'yyyy-mm-dd HH:MM:ss');
        }
    },

    updatedAt: {
        type: Sequelize.DATE,
        get() {
            const rawValue = this.getDataValue('updatedAt');
            return dateFormat(rawValue, 'yyyy-mm-dd HH:MM:ss');
        }
    }
}, {
    // 是否支持驼峰
    underscored: false,
    // MySQL数据库表名
    tableName: 'goodClass',
});
/***商品分类 结束 */



(async () => {
    good.sync({
        alter: true
    });

    goodClass.sync({
        alter: true
    });

    //console.log("教程同步成功")
})();

module.exports = {
    good,
    goodClass
};